home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3116 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  91 lines

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: Bowden Wise <wiseb@cs.rpi.edu>
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Static STL containers ?  Possible ?
  5. Date: 22 Jan 1996 09:52:44 -0000
  6. Organization: Rensselaer Polytechnic Institute, Computer Science
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: devitto@ferndown.ate.slb.com
  9. Message-ID: <4dvmpc$2eb@netlab.cs.rpi.edu>
  10. NNTP-Posting-Host: netlab.cs.rpi.edu
  11. X-Original-Date: Mon, 22 Jan 1996 01:13:28 -0800
  12.  
  13. I am creating a singleton class registry.  To implement this, 
  14. I have created a base singleton class, called Singleton.  All of
  15. the other singleton classes are derived from this one, and must
  16. "Register" themselves by calling the base class method:
  17.  
  18.   Singleton::Register (const char* name, Singleton* instance);
  19.  
  20. The registry is an STL map:
  21.  
  22.   static map<string, Singleton*, less<string> > _registry;
  23.  
  24. which is a static member of the base class Singleton.
  25.  
  26. Clearly there is only one instance of each derived singleton.  Say
  27. classes A and B are two such derived classes of Singleton.  Then I
  28. have tried creating their instances by placing this:
  29.  
  30. static A instanceA;
  31. static B instanceB;
  32.  
  33. before main:
  34.  
  35. void main ()
  36. {
  37.    // Lookup singleton A:
  38.    Singleton* a = Singleton::Lookup ( "A" );
  39.  
  40.    if ( a )
  41.    {
  42.    }
  43.  
  44. }
  45.  
  46. The static instances of A and B are created BEFORE main() exectues
  47. as expected, however, it aborts during the construction of A.
  48.  
  49. I get an abort, inside one of the STL tree routines.  
  50.  
  51. Now, if I move the statics *inside* main:
  52.  
  53. void main ()
  54. {
  55.    // statics now in main()
  56.    static A instanceA; 
  57.    static B instanceB;
  58.  
  59.    // Lookup singleton A:
  60.    Singleton* a = Singleton::Lookup ( "A" );
  61.  
  62.    if ( a )
  63.    {
  64.    }
  65.  
  66. }
  67.  
  68. then the aborts do not occur.
  69.  
  70. Can someone explain why this is the case?  
  71.  
  72. I am thinking that since C++ does not gurantee the order of the 
  73. construction of statics, 
  74. and  since the STL relies heavily on static variables, that my statics
  75. must be getting constructed before all of the STL static vars have 
  76. been initialized?
  77.  
  78. Is that possible?  What is the solution?  Do I have to create
  79. the instances via another class that perhaps 'new''s the single
  80. instances ?
  81.  
  82. --------------------------------------------------------------------
  83. G. Bowden Wise
  84. Computer Science Dept, Rensselaer Polytechnic Inst, Troy, NY 12180
  85. Email: wiseb@cs.rpi.edu         WWW: http://www.cs.rpi.edu/~wiseb/
  86.  
  87.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  88.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  89.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  90.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  91.